package com.kingyea.mobilepolice.apimanage.system.sync;

import com.alibaba.fastjson.JSON;
import com.carrotsearch.sizeof.RamUsageEstimator;
import com.kingyea.mobilepolice.apimanage.interfaceflow.bean.InterfaceFlow;
import com.kingyea.mobilepolice.apimanage.interfaceflow.service.InterfaceFlowService;
import com.kingyea.mobilepolice.apimanage.serviceinfo.bean.ServiceInfo;
import com.kingyea.mobilepolice.apimanage.serviceinfo.service.ServiceInfoService;
import com.kingyea.mobilepolice.apimanage.sysvo.bean.RequestObj;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @author cxc
 * @version v1.0.0
 * @description
 * @date Created in 2018-05-05 17:34
 */
@Component
public class InterfaceFlowTask {

    private static final Logger LOGGER = LoggerFactory.getLogger(InterfaceFlowTask.class);

    @Autowired
    private InterfaceFlowService interfaceFlowService;

    @Autowired
    private ServiceInfoService serviceInfoService;

    /**
     * @Description: 请求流量统计
     * @param: []
     * @return: void
     * @author: cxc
     * @Date: 2018-05-05
     */
    @Async  //加入"异步调用"注解
    public void requestFlowCollect(RequestObj reqObj) {
        boolean flag = Boolean.FALSE;
        try {
            // 1 -> 查询service_info
            // 2 -> 查询service_requester-association
            // 3 -> 查询requester请求方
            InterfaceFlow bean = new InterfaceFlow();
            bean.setType(1);
            bean.setTypeUrlId(reqObj.getServiceId());
            bean.setTypeId(reqObj.getRequestId());
            bean.setInterfaceByte(Long.valueOf(RamUsageEstimator.sizeOf(reqObj.getParams())).intValue());
//            bean.setCreateTime(new Date());
            flag = interfaceFlowService.save(bean);
        } catch (Exception e) {
            //不处理
            e.printStackTrace();
            LOGGER.error("【请求方流量统计异常】", e);
        }
        LOGGER.info("【请求方流量统计】" + flag + "：" + JSON.toJSONString(reqObj));
    }

    /**
     * @Description: 响应流量统计
     * @param: [requestParam, responseBody]
     * @return: void
     * @author: cxc
     * @Date: 2018-05-05
     */
    @Async
    public void respohnseFlowCollect(RequestObj reqObj, Object map) {
        boolean flag = Boolean.FALSE;
        try {
            // 1 -> 查询service_info
            // 2 -> 查询provider提供方
            InterfaceFlow bean = new InterfaceFlow();
            bean.setType(2);
            bean.setTypeId(Optional.ofNullable(serviceInfoService.getById(reqObj.getServiceId())).map(ServiceInfo::getProviderId).orElse(-1));
            bean.setTypeUrlId(reqObj.getServiceId());
            bean.setInterfaceByte(Long.valueOf(RamUsageEstimator.sizeOf(map)).intValue());
//            bean.setCreateTime(new Date());
            flag = interfaceFlowService.save(bean);
        } catch (Exception e) {
            //不处理
            e.printStackTrace();
            LOGGER.error("【提供方流量统计异常】", e);
        }
        LOGGER.info("【提供方流量统计】" + flag + "：" + JSON.toJSONString(reqObj));
    }

}
